package com.hospital.doctor.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

/**
 * @author QIUYANG
 * @title: JWTUtiles
 * @description: TODO
 * @date 2021/5/610:51
 */
public class JWTUtils {

    //定义签名
    private static final String SIGN = "qwe123";

    //生成token
    public static String getToken(Map<String,String> map){
        //设置过期时间
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE,10);
        //创建jwt builder
        JWTCreator.Builder builder = JWT.create();
        map.forEach((k,v)->{
            builder.withClaim(k,v);
        });
        String token = builder.withExpiresAt(instance.getTime())
                .sign(Algorithm.HMAC256(SIGN));

        return token;
    }

    //验证token
    public static DecodedJWT verify(String token){
        System.out.println("验证token");
         return JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
    }





}
